
namespace Behemoth.SharePoint.CustomFieldTypes {

    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.SharePoint;

	public class SPFieldMultiColumnValueTools {

		public static string TrimMultiColumnFormatString( string multiColumnFormatString, bool trimStart, bool trimEnd ) {
			if (trimStart && multiColumnFormatString.StartsWith( SPFieldMultiChoiceValue.Delimiter ))
				multiColumnFormatString = multiColumnFormatString.Substring( SPFieldMultiChoiceValue.Delimiter.Length );
			if (trimEnd && multiColumnFormatString.EndsWith( SPFieldMultiChoiceValue.Delimiter ))
				multiColumnFormatString = multiColumnFormatString.Substring(0, multiColumnFormatString.Length - SPFieldMultiChoiceValue.Delimiter.Length );
			return multiColumnFormatString;
		}

		public static string CanonicalizeMultiColumnFormatString( string multiColumnFormatString ) {
			// check to make sure our string both starts and ends with a delimiter, as it should
			if (!multiColumnFormatString.StartsWith( SPFieldMultiChoiceValue.Delimiter ))
				multiColumnFormatString = SPFieldMultiChoiceValue.Delimiter + multiColumnFormatString;
			if (!multiColumnFormatString.EndsWith( SPFieldMultiChoiceValue.Delimiter ))
				multiColumnFormatString += SPFieldMultiChoiceValue.Delimiter;
			// you should never have a situation where you have an empty string, or a string with only one delimeter 
			// in it... we put this at the end in case we get a short one with no delimiters on it, like "1"
			if (multiColumnFormatString.Length < SPFieldMultiColumnValue.Delimiter.Length * 2)
				multiColumnFormatString = SPFieldMultiColumnValue.Delimiter + SPFieldMultiColumnValue.Delimiter;
			return multiColumnFormatString;
		}

		public static bool IsMultiColumnFormatString( string multiColumnFormatString ) {
			return (multiColumnFormatString.IndexOf( SPFieldMultiColumnValue.Delimiter ) >= 0);
		}

	} // class

} // namespace
